/* ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
   Module Name          : com.yaodian100.ec.combo.web.ComboProductAction
   Module Description   :

   Date Created      : 2009/12/10
   Original Author   : eric.chen
   Team              : yaodian100
   ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
   MODIFICATION HISTORY
   ------------------------------------------------------------------------------
   Date Modified       Modified by       Comments
   ------------------------------------------------------------------------------
   ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
package com.yaodian100.ec.comboproduct.web;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.Preparable;
import com.yaodian100.core.common.dao.impl.CommonCriteria;
import com.yaodian100.core.common.entity.SimplePager;
import com.yaodian100.core.common.utils.DateUtil;
import com.yaodian100.ec.comboproduct.entity.ComboProduct;
import com.yaodian100.ec.comboproduct.service.ComboProductService;

/**
 * @author eric.chen
 * 
 */
public class ComboProductAction extends ActionSupport implements Preparable {

	private static final long serialVersionUID = 178887580319281383L;
	/** logger */
	private final Logger logger = LoggerFactory.getLogger(ComboProductAction.class);

	private ComboProductService comboProductService;
	private SimplePager pager;

	private List<ComboProduct> comboProductList;
	private ComboProduct comboProduct;

	private String mainProductID;
	private String addonProductID;
	private BigDecimal addonPrice;
	private Date startDateTime;
	private String startDate;
	private String startTime;
	private Date endDateTime;
	private String endDate;
	private String endTime;
	private String enable;

	/* 由FORM传入之查询条件 */
	private String mainProductIDParam;
	private String addonProductIDParam;
	private String comboStatusParam;

	public String list() throws Exception {

		CommonCriteria crit = new CommonCriteria();

		if (!StringUtils.isBlank(mainProductIDParam))
			crit.addEq("campaignType", mainProductIDParam);
		if (!StringUtils.isBlank(addonProductIDParam))
			crit.addEq("displayTiming", addonProductIDParam);
		if (!StringUtils.isBlank(comboStatusParam)) {

			if (comboStatusParam.trim().equals("CLOSED")) {
				crit.addGe("startDateTime", new Date());
			}
			if (comboStatusParam.trim().equals("ONAIR")) {
				crit.addGe("startDateTime", new Date());
				crit.addLe("endDateTime", new Date());
			}
			if (comboStatusParam.trim().equals("READY")) {
				crit.addLe("startDateTime", new Date());
			}
		}

		if (pager == null)
			pager = new SimplePager();
		// pager.setPageRecord(1);
		int entitySize = comboProductService.getListSize(crit).intValue();
		pager.setTotalSize(entitySize);

		comboProductList = comboProductService.getList(pager.getCurrentPage() * pager.getPageRecord(), pager
				.getPageRecord(), crit, null);

		// logger.info("pager:{},campaignList:{}", pager, campaignList);

		return "list";
	}

	public String save() throws Exception {

		try {

			comboProduct = new ComboProduct();

			if (!StringUtils.isBlank(this.mainProductID))
				comboProduct.setMainProductID(mainProductID);

			if (!StringUtils.isBlank(this.addonProductID))
				comboProduct.setAddonProductID(addonProductID);

			if (!StringUtils.isBlank(this.startDate) && !StringUtils.isBlank(this.startTime))
				comboProduct.setStartDateTime(DateUtil.convertStringToDate("yyyy/MM/dd HH:mm", this.startDate + " "
						+ this.startTime));
			if (this.endDate != "" && this.endTime != "")
				comboProduct
						.setEndDateTime(DateUtil.convertStringToDate("yyyy/MM/dd HH:mm", this.endDate + " " + this.endTime));

			comboProduct.setAddonPrice(addonPrice);

			if (!StringUtils.isBlank(this.enable))
				comboProduct.setEnable(enable);

			comboProductService.save(comboProduct);

		} catch (Exception e) {
			this.addActionError("储存失败：" + e.getMessage());
			e.printStackTrace();
			return "edit";
		}
		return "list";
	}

	/**
	 * @return the startDate
	 */
	public String getStartDate() {
		return startDate;
	}

	/**
	 * @return the startTime
	 */
	public String getStartTime() {
		return startTime;
	}

	/**
	 * @return the endDate
	 */
	public String getEndDate() {
		return endDate;
	}

	/**
	 * @return the endTime
	 */
	public String getEndTime() {
		return endTime;
	}

	/**
	 * @param startDate the startDate to set
	 */
	public void setStartDate(String startDate) {
		this.startDate = startDate;
	}

	/**
	 * @param startTime the startTime to set
	 */
	public void setStartTime(String startTime) {
		this.startTime = startTime;
	}

	/**
	 * @param endDate the endDate to set
	 */
	public void setEndDate(String endDate) {
		this.endDate = endDate;
	}

	/**
	 * @param endTime the endTime to set
	 */
	public void setEndTime(String endTime) {
		this.endTime = endTime;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.opensymphony.xwork2.Preparable#prepare()
	 */
	
	public void prepare() throws Exception {
		// TODO Auto-generated method stub

	}

	/**
	 * @return the logger
	 */
	public Logger getLogger() {
		return logger;
	}

	/**
	 * @return the comboProductService
	 */
	public ComboProductService getComboProductService() {
		return comboProductService;
	}

	/**
	 * @return the pager
	 */
	public SimplePager getPager() {
		return pager;
	}

	/**
	 * @return the comboProductList
	 */
	public List<ComboProduct> getComboProductList() {
		return comboProductList;
	}

	/**
	 * @return the comboProduct
	 */
	public ComboProduct getComboProduct() {
		return comboProduct;
	}

	/**
	 * @return the mainProductID
	 */
	public String getMainProductID() {
		return mainProductID;
	}

	/**
	 * @return the addonProdcutID
	 */
	public String getAddonProdcutID() {
		return addonProductID;
	}

	/**
	 * @return the addonPrice
	 */
	public BigDecimal getAddonPrice() {
		return addonPrice;
	}

	/**
	 * @return the startDateTime
	 */
	public Date getStartDateTime() {
		return startDateTime;
	}

	/**
	 * @return the endDateTime
	 */
	public Date getEndDateTime() {
		return endDateTime;
	}

	/**
	 * @param comboProductService the comboProductService to set
	 */
	public void setComboProductService(ComboProductService comboProductService) {
		this.comboProductService = comboProductService;
	}

	/**
	 * @param pager the pager to set
	 */
	public void setPager(SimplePager pager) {
		this.pager = pager;
	}

	/**
	 * @param comboProductList the comboProductList to set
	 */
	public void setComboProductList(List<ComboProduct> comboProductList) {
		this.comboProductList = comboProductList;
	}

	/**
	 * @param comboProduct the comboProduct to set
	 */
	public void setComboProduct(ComboProduct comboProduct) {
		this.comboProduct = comboProduct;
	}

	/**
	 * @param mainProductID the mainProductID to set
	 */
	public void setMainProductID(String mainProductID) {
		this.mainProductID = mainProductID;
	}

	/**
	 * @param addonProdcutID the addonProdcutID to set
	 */
	public void setAddonProductID(String addonProdcutID) {
		this.addonProductID = addonProdcutID;
	}

	/**
	 * @param addonPrice the addonPrice to set
	 */
	public void setAddonPrice(BigDecimal addonPrice) {
		this.addonPrice = addonPrice;
	}

	/**
	 * @param startDateTime the startDateTime to set
	 */
	public void setStartDateTime(Date startDateTime) {
		this.startDateTime = startDateTime;
	}

	/**
	 * @param endDateTime the endDateTime to set
	 */
	public void setEndDateTime(Date endDateTime) {
		this.endDateTime = endDateTime;
	}

	/**
	 * @return the enable
	 */
	public String getEnable() {
		return enable;
	}

	/**
	 * @param enable the enable to set
	 */
	public void setEnable(String enable) {
		this.enable = enable;
	}
}
